<?php

require 'runtime.php';

@$d = $_GET['d'];
if($d == '0') $d = date("Y-m-d");
$d = strtotime($d);
if(!$d) $d = '';
else $d = date("Y-m-d", $d);

$db = init_db();

$data = array();
$categories = array();

if(!strlen($d)){
	// 最近30天记录
	$row = array();
	$maxnum = 0;
	$data1 = $db->get_rows("select * from weather_count where `date`>='".date("Y-m-d", time()-86400*30)."' and `table` in('wcc','ics','spb','xml') order by `date`");
	foreach($data1 as $r){
		$d = date("d", strtotime($r['date']));
		if(!isset($row[$r['date']][0])) $row[$r['date']][0] = 0;
		if(!isset($row[$r['date']][1])) $row[$r['date']][1] = 0;
		if(!isset($row[$r['date']][2])) $row[$r['date']][2] = 0;
		if(!isset($row[$r['date']][3])) $row[$r['date']][3] = 0;
		if($r['table'] == 'wcc'){
			$row[$r['date']][0] += $r['nums'];
		}elseif($r['table'] == 'ics'){
			$row[$r['date']][1] += $r['nums'];
		}elseif($r['table'] == 'spb'){
			$row[$r['date']][2] += $r['nums'];
		}elseif($r['table'] == 'xml'){
			$row[$r['date']][3] += $r['nums'];
		}
		
		$r["d"] = date("d", strtotime($r["date"]));
		if(!in_array($r["d"], $categories)) $categories[] = $r["d"];
		
		if($row[$r['date']][1] > $maxnum) $maxnum = $row[$r['date']][1];
		if($row[$r['date']][2] > $maxnum) $maxnum = $row[$r['date']][2];
	}
	
	foreach($row as $r){
		$data[0][] = $r[0];
		$data[1][] = $r[1];
		$data[2][] = $r[2];
		$data[3][] = $r[3];
	}
	$title = '最近30天更新信息';

}else{
	
	for($i=0; $i<=23; $i++) $row[$i] = array(0 , 0, 0, 0);
	$maxnum = 0;
	$data1 = $db->get_rows("select * from weather_count where `date`='{$d}' and `table` in('wcc','ics','spb','xml') order by `date`");
	foreach($data1 as $r){
		if($r['table'] == 'wcc'){
			$row[$r['hour']][0] += $r['nums'];
		}elseif($r['table'] == 'ics'){
			$row[$r['hour']][1] += $r['nums'];
		}elseif($r['table'] == 'spb'){
			$row[$r['hour']][2] += $r['nums'];
		}elseif($r['table'] == 'xml'){
			$row[$r['hour']][3] += $r['nums'];
		}
		if($r['nums'] > $maxnum) $maxnum = $r['nums'];
		
		if(!in_array($r["hour"], $categories)) $categories[] = $r["hour"];
		
	}
	
	foreach($row as $r){
		$data[0][] = isset($r[0]) ? $r[0] : 0;
		$data[1][] = isset($r[1]) ? $r[1] : 0;
		$data[2][] = isset($r[2]) ? $r[2] : 0;
		$data[3][] = isset($r[3]) ? $r[3] : 0;
	}
	$title = $d." 24小时更新信息";
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
<title>分流服务器使用记录</title>
<script type="text/javascript" src="/scripts/jquery.min.js"></script>
<script type="text/javascript" src="/scripts/highcharts.js"></script>
<script type="text/javascript" src="/scripts/exporting.js"></script>
<style type="text/css">
body {font-size: 12px;	margin:2px;background-color:#ffffff;}
}
</style>
</head>
<body>
<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>

<script type="text/javascript">
var chart = new Highcharts.Chart({
	chart: {
		renderTo: 'container',
		type: 'line',
		marginRight: 130,
		marginBottom: 25
	},
	title: {
		text: "<?php echo $title?>",
		x: -20 //center
	},
	subtitle: {
		text: '信息来源: http://w.mdeve.com',
		x: -20
	},
	xAxis: {
		categories: [<?php echo "'". implode("','", $categories), "'" ?>]
	},
	yAxis: {
		title: {
			text: '更新量数据'
		},
		plotLines: [{
			value: 0,
			width: 1,
			color: '#808080'
		}]
	},
	
	legend: {
		layout: 'vertical',
		align: 'right',
		verticalAlign: 'top',
		x: -10,
		y: 100,
		borderWidth: 0
	},
	series: [{
		name: 'WM6.5 Manila',
		data: [<?php echo implode(",", $data[0])?>]
	}, {
		name: 'iCalendar',
		data: [<?php echo implode(",", $data[1])?>]
	}, {
		name: 'Spb Weather',
		data: [<?php echo implode(",", $data[2])?>]
	}, {
		name: 'Interface',
		data: [<?php echo implode(",", $data[3])?>]
	}]
});
</script>
</body>
</html>